<%@ page language="java"  contentType="text/html;charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>角色管理-授权资源</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script type="text/javascript" src="${ctx }/extjs/easyext/ux/TreeCheckNodeUI.min.js"></script>
<script language="javascript" type="text/javascript">
function closeCurrentWindow(result) {
	var winId='${winId}';
	var closeWin=Ext.WindowMgr.get(winId);
	if(closeWin==null) {
		closeWin=parent.Ext.WindowMgr.get(winId);
	}
	if(closeWin==null) {
		window.returnValue=result;
		window.close();
	}else {
		closeWin.returnValue=result;
		closeWin.close();
	}
}
Ext.onReady(function() {
	var treePanel = new Ext.tree.TreePanel({
		style: 'text-align:left;',
		autoScroll: true,
		frame: false,
		enableDD: true,
		containerScroll: true,
		rootVisible: false,
		border: false,
		animate: true,
		onlyLeafCheckable: true,
		bbar: [{
			iconCls: 'acceptBtn',
			text: '保存',
			handler: function () {
				//JSON字符串
				var resourceJSONString='';
				treePanel.root.cascade(function(n) {
					if(n.attributes.checked) {
						resourceJSONString+=',{';
						resourceJSONString+='resourceId:'+n.id;
						resourceJSONString+=',operationIds:[';
						var operationIds='';
						var es=document.getElementsByName("checkbox_operation_" + n.id);
						for(var i=0; i<es.length;i++) {
							if(es[i].checked) {
								operationIds+=','+es[i].value;
							}
						}
						if(!Ext.isEmpty(operationIds)) {
							resourceJSONString+=operationIds.substring(1);
						}
						resourceJSONString+=']';
						resourceJSONString+='}';
					}
				    return true;
				});
				resourceJSONString=Ext.isEmpty(resourceJSONString)?'[]':('['+resourceJSONString.substring(1)+']');
				Common.ajaxRequest('${ctx}/role!resetResourceJSON.action',{
					id: '${id}',
					resourceJSONString: resourceJSONString
				},function(success) {
					if(success) {
						closeCurrentWindow(true);
					}
				});
			}
		},'-',{
			iconCls: 'delete2Btn',
			text: '取消',
			handler: function() {
				closeCurrentWindow(false);
			}
		}],
	    root: new Ext.tree.AsyncTreeNode({
			id: '1',
			text: 'ROOT',
			leaf: false,
			expanded: true
	    }),
		loader: new Ext.tree.TreeLoader({
			url: '${ctx}/role!listResourceJSON.action',
			baseAttrs: {
				uiProvider: Ext.ux.TreeCheckNodeUI
			},
			baseParams: {
				id: '${id}'
			}
		})
	});
	
	var viewport = new Ext.Viewport({
		layout: 'fit',
		items: [treePanel]
	});
});
</script>
</head>

<body>
</body>
</html>
